<?

/** dzkone readset functionality.
 *
 *  This collection of functions (libary) is licensed under GPL2.
 *  See LICENSE or www.gnu.org for more details.
 *
 *  @author: JQ + Henri Ranki 11.6.2001
 *
 *  $Id: dzkone_list.php,v 1.7 2005/11/10 19:31:04 jyry Exp $
 */


/* Include files */
include_once("sodebug.php");


/** Performs a list request based on data in DZK_DETAILS.
 *  @param dzKone $Kone dzKone object to operate with.
 *  @param bool $Fresh When set(1), causes query run without checking for cached
 *      browse list.
 *  @param bool true(1) if list was run successfully, otherwise false(0)
 */
function __dzkone__list(&$Kone, $Fresh = 0) {
    include("sodebug.inc");

    /* get datacols from DETAILS, columns to fetch  */
    $data_cols = $Kone->__InItu->getBlockItem(DZI_CONTROL, DZK_DETAILS);
    if (!count($data_cols)) {
        soDebug("__dzkone_list skipped with invalid ini data: ".
                $data_cols.", no items", SOD_DET);
        return 0; // <-- EXIT
    }; // else if ini ok )

    /* return browse list keys from file or run the query again */
    if (!$Fresh) {
        $is = &new soInputStream($Kone->__getListFileName(
                                 $Kone->__Purkki->getStore(TISKI)));
        if ($is->Error) $Fresh = 1;
    }; // if

    if ($Fresh) {
        $Kone->__query();
        $is = &new soInputStream($Kone->__getListFileName(
                                 $Kone->__Purkki->getStore(TISKI)));
    } else {
        soDebug("dzKone->__list: restoring from browse cache", SOD_LO);
    }; // else if
    $Kone->__Purkki->setStore(TISKI, unserialize($is->Data));

    /* At this point we ought to have browse keys in TISKI. */
    return __dzkone__list_sub($Kone, $data_cols);
} // __dzkone__list


/** Actual list functionality.
 *  Called by __list which has sort out browse keys from file or by
 *  querying to TAFFEL.
 *  @param dzKone $Kone dzKone object to operate with.
 *  @param array/string $DataCols Data column names as an array.
 */
function __dzkone__list_sub(&$Kone, $DataCols) {
    include("sodebug.inc");
    soDebug("dzKone->__list_sub2 entered, datacols='".
            implode(" ", $DataCols)."'", SOD_DET);
    $Kone->renameAliases(TISKI);
    $Kone->__Purkki->initStore(TAFFEL);

    /* rename aliases in data columns */
    $kcount = count($DataCols);
    for ($k = 0; $k < $kcount; $k++) {
        $name = $Kone->__Struct->findCol($DataCols[$k], 1);
        if ($name != "") {
            $DataCols[$k] = $name;
        }; // if
    }; // for

    /* initialise soda */
    $Kone->__Soda->clearAllSections();
    $Kone->__Soda->setAction(SODA_SELECT);
    $Kone->__Soda->setSection(SODA_DATA, implode(" ", $DataCols), array());
    $Kone->__Soda->setTable(soExtractVal($DataCols[0], ".", 1));


    /* Sort out datatypes to $cols_d */
    $cols = $Kone->__Purkki->getColNames(TISKI);
    $cols_d = array();
    foreach ($cols as $col) {
        $cols_d[] = $Kone->__Struct->getDatatype($col);
    }; // foreach
    $ccount = count($cols);

    /* Loop browse list */
    $rcount = $Kone->__Purkki->getRowCount(TISKI, implode(" ", $cols));
    $dcols = array();
    $dvals = array();
    $pcols = array();
    $pvals = array();
    for ($r = 0; $r < $rcount; $r++) {

        for ($c = 0; $c < $ccount; $c++) {
            $col = $cols[$c];
            $dcols[] = $col."#".$r.":".$cols_d[$c];
            $dvals[] = $Kone->__Purkki->getStoreItem(TISKI, $col, $r);
        }; // foreach

    }; // for
    $Kone->__Soda->setSection(SODA_WHERE, $dcols, $dvals);

    for ($r = 0; $r < $rcount; $r++) {

        for ($c = 0; $c < $ccount; $c++) {
            $col = $cols[$c];
            $dtype = ":".$cols_d[$c];
            $Kone->__Soda->addToSection(SODA_WHERE, $col."#".$r.$dtype,
                $Kone->__Purkki->getStoreItem(TISKI, $col, $r));
        }; // foreach
        $Kone->__Soda->addToSection(SODA_WHERE_ORS,
                                    $cols[$ccount -1]."#".$r, 1);
        if ($ccount > 1) {
            $Kone->__Soda->addToSection(SODA_PARENTHESES,
                                        $cols[0]."#".$r, "1,0");
            $start = 0;
        } else {
            $start = 1;
        };
        $Kone->__Soda->addToSection(SODA_PARENTHESES,
                                    $cols[$ccount -1]."#".$r, $start.",1");
    }; // for

    $sorts = $Kone->__Struct->getSorting();
    foreach ($sorts as $col => $value) {
        $dtype = $Kone->__Struct->getDatatype($col);
        $Kone->__Soda->addToSection(SODA_ORDER, $col.":".$dtype, $value);
    }; // foreach

    if ($Kone->__Soda->run()) {
        foreach ($DataCols as $data_col) {
            /* Add values to the end of a existing column --or--
            * setup as a new column. */
            if ($Kone->__Purkki->getStoreCol(TAFFEL, $data_col) == NULL) {
                $Kone->__Purkki->setStoreCol(TAFFEL, $data_col,
                    $Kone->__Soda->Results->getCol($data_col));
            } else {
                $Kone->__Purkki->addToStoreCol(TAFFEL, $data_col,
                    $Kone->__Soda->Results->getCol($data_col));
            }; // else if
        }; // foreach
    } else {
        $rcount = -1;
    }; // if run ok

    /* return keycount as result */
    $Kone->__OutItu->setBlockItem(DZI_CONTROL, DZK_RESULT, $rcount);
    return $rcount;
} // __dzkone__list_sub

?>
